﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class XmlStorageManager
	{
		private XmlStorageManager()
		{
		}

		private static XmlStorageManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the XmlStorageManager.
		/// </summary>
		public static XmlStorageManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new XmlStorageManager();
						}
					}
				}
				return instance;
			}
		}

		private dtoXmlStorage _storage = null;
		public dtoXmlStorage Storage
		{
			get
			{
				if(_storage == null)
				{
					_storage = new dtoXmlStorage();

					if (!File.Exists(XMLStorageConfiguration.Instance.FilePathToStorage))
					{
						_storage.WriteXml(XMLStorageConfiguration.Instance.FilePathToStorage);
					}

					_storage.ReadXml(XMLStorageConfiguration.Instance.FilePathToStorage);
				}

				return _storage;
			}
		}

		public void AcceptChanges()
		{
			Storage.AcceptChanges();
			Storage.WriteXml(XMLStorageConfiguration.Instance.FilePathToStorage);
		}
	}
}
